package tool

import (
	"fmt"
	"regexp"
	"strconv"
)

type Upgrade struct {
	Level int   `json:"level" redis:"level"`
	Exp   int   `json:"exp" redis:"exp"`
	Cost  []int `json:"cost" redis:"cost"`
}

//转换成结构体存入
func Dataprocess(rows [][]string) (u []Upgrade, err error) {
	for i, row := range rows {
		var uv Upgrade
		if i <= 2 {
			continue
		}
		for k, rowv := range row {
			switch {
			case k == 0:
				uv.Level, err = strconv.Atoi(rowv)
				if err != nil {
					return
				}
			case k == 1:
				uv.Exp, err = strconv.Atoi(rowv)
				if err != nil {
					return
				}
			case k == 2:
				uv.Cost, err = StoInts(rowv)
				if err != nil {
					return
				}
			default:
				err = fmt.Errorf("数据格式不符合规则")
				return nil, err
			}
		}
		u = append(u, uv)
	}
	return
}

//字符串:[v,v]转数组
func StoInts(s string) (cost []int, err error) {
	regexp1 := regexp.MustCompile(`\d+`)
	costr := regexp1.FindAllStringSubmatch(s, -1)
	for _, v := range costr {
		for _, v1 := range v {
			res, err := strconv.Atoi(v1)
			if err != nil {
				return nil, err
			}
			cost = append(cost, res)
		}
	}
	return cost, err
}
